package com.turing.draw.controller;

import com.turing.common.entity.Context;
import com.turing.common.response.*;
import com.turing.common.util.AssertUtil;
import com.turing.common.util.ResponseUtil;
import com.turing.draw.model.vo.UploadVo;
import com.turing.draw.vo.out.DrawTaskVO;
import com.turing.draw.enums.TaskResultCode;
import com.turing.draw.service.DrawTaskService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Description Controller
 * @Author lcy
 * @Date 2021-10-25
 */
@RestController
@RequestMapping("/draw")
@Api(tags = {"服务入口"})
public class DrawTaskController {

    /**
     * 接口
     **/
    @Autowired
    private DrawTaskService drawTaskService;

    /**
     * 要素抽取 | 选择场景文件上传接口
     * @param files
     * @param templateId
     * @param needMerge
     * @param pageNumber
     * @return
     * @throws IOException
     */
    @PostMapping("uploadFileAndCreateTask")
    public ResponseVO<UploadVo> uploadFileAndCreateTask(@RequestParam(value = "file") MultipartFile[] files,
                                                        @RequestParam("templateId") String templateId, @RequestParam("needMerge") Integer needMerge,
                                                        Integer pageNumber) throws IOException {

        AssertUtil.isTrue(!Context.getTokenContext().getTemplates().containsKey(templateId), CommonCode.NOT_AUTHORIZATION);

        if (pageNumber == null) {
            pageNumber = 0;
        }

        AssertUtil.isEmpty(files, TaskResultCode.FILE_NOT_UPLOADED);

        return ResponseUtil.success(drawTaskService.uploadFileAndCreateTask(files, templateId, needMerge, pageNumber));

    }

    /*
     * 对内-获取任务及子任务PDF文件地址
     * taskSn 父任务id
     * */
    /*
     * 对内-获取任务及子任务PDF文件地址
     * taskSn 父任务id
     * */
    @GetMapping("getTaskFilePath")
    public ResponseVO<DrawTaskVO> getTaskFilePath(@RequestParam("taskSn") String taskSn){
        DrawTaskVO task = drawTaskService.getTaskFilePath(taskSn);
        ResponseVO<DrawTaskVO> responseVO = new ResponseVO<>();
        responseVO.setMessage(ResponseMessage.SUCCESS);
        responseVO.setStatus(0);
        responseVO.setData(task);
        return responseVO;
    }

    @GetMapping("getTaskTemplate")
    public String getTaskTemplate(@RequestParam("taskSn") String taskSn){
        return drawTaskService.getTaskTemplate(taskSn);
    }

    @PostMapping("removeBatch")
    public ResponseVO<Object> removeBatch(@RequestBody List<String> taskSns){
        return ResponseUtil.success(drawTaskService.removeBatch(taskSns));
    }

}